package org.deadline.model.config;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.deadline.model.baseapp.BaseEntity;
import org.deadline.model.baseapp.Organization;
import org.deadline.model.user.Permission;
import org.deadline.model.user.Person;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

@Entity
public class Role extends BaseEntity  implements Serializable {

    @NotNull
    @Size(min = 6, max = 255)
    private String name;

    @NotNull
    @Size(min = 6, max = 255)
    private String description;

    @ManyToOne
    private Organization organization;

    @ManyToOne
    private Person owner;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "role")
    private Set<Permission> permissions = new HashSet<Permission>();

	@Id
	@Column(name="id",unique=true,nullable=false)
	@SequenceGenerator(name="role_seq_gen",sequenceName="role_seq")
	@GeneratedValue(generator="role_seq_gen",strategy=GenerationType.TABLE)
	@Generated(GenerationTime.INSERT)
	private Long id;

	
	private static final long serialVersionUID = 1L;

	public String getName() {
        return this.name;
    }

	public void setName(String name) {
        this.name = name;
    }

	public String getDescription() {
        return this.description;
    }

	public void setDescription(String description) {
        this.description = description;
    }

	public Organization getOrganization() {
        return this.organization;
    }

	public void setOrganization(Organization organization) {
        this.organization = organization;
    }

	public Person getOwner() {
        return this.owner;
    }

	public void setOwner(Person owner) {
        this.owner = owner;
    }

	public Set<Permission> getPermissions() {
        return this.permissions;
    }

	public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }

	public String toJson() {
        return new JSONSerializer().exclude("*.class").deepSerialize(this);
    }

	public static Role fromJsonToRole(String json) {
        return new JSONDeserializer<Role>().use(null, Role.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Role> collection) {
        return new JSONSerializer().exclude("*.class").deepSerialize(collection);
    }

	public static Collection<Role> fromJsonArrayToRoles(String json) {
        return new JSONDeserializer<List<Role>>().use(null, ArrayList.class).use("values", Role.class).deserialize(json);
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public Long getId() {
	    return this.id;
	}

	public void setId(Long id) {
	    this.id = id;
	}

	}
